#!/usr/bin/env python2.5
################################################################################
#
#   sequence_locus.py
# 
# 
#       t_locus
# 
#           chromosome, beg, end, strand
#
#
#   Copyright (C) 2007 Leo Goodstadt
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; version 2
#   of the License
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#################################################################################



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   t_locus

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
class t_locus (object):
    def __init__ (self, chromosome, beg, end, strand):
        self.beg = beg
        self.len = end - beg
        self.strand = strand
        self.chromosome = chromosome
    indent = "        "
    strand_plusminus_strs = ['-', '+']

    def tab_delimited (self):
        return "\t".join(map(str,  [self.chromosome, 
                                    self.beg, 
                                    self.beg + self.len,
                                    int(self.strand)]))
                                    
    def __str__ (self):
        if self.strand:
            strand = '+'
        else:
            strand = '-'
        return t_locus.indent + "[%s %d-%d [%d] %s]" % (self.chromosome, self.beg, 
                                                        self.beg + self.len,  
                                                        self.len, 
                                                        t_locus.strand_plusminus_strs[self.strand])
    def __cmp__ (self, other):
        if other == None:
            return -1
        return (
                cmp(self.chromosome,  other.chromosome) or 
                cmp(self.beg,  other.beg) or 
                cmp(self.len,  other.len) or 
                cmp(self.strand,  other.strand))
        
    def __hash__ (self):
        return str(self).__hash__()


import copy
def  merge_overlapping_loci(loci):
    """

    return list with overlapping t_loci merged
        
    """

    #
    #   nothing to merge
    #
    if not len(loci):
        return list()

    loci = sorted(list(loci))
    
    merged_loci = list()
    merged_loci.append(copy.copy(loci[0]))

    for locus in loci[1:]:
        last_merged_locus = merged_loci[-1]
        #
        #  overlap: expand current locus
        if (    locus.chromosome == last_merged_locus.chromosome and
                locus.strand     == last_merged_locus.strand     and
                locus.beg <= merged_loci[-1].beg + merged_loci[-1].len):
            last_merged_locus.len = locus.beg + locus.len - last_merged_locus.beg
        else:
            merged_loci.append(copy.copy(locus))        
    return merged_loci
    



